/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package base;

import java.util.Hashtable;

/**
 *
 * @author Tue
 */
public class TrackForest {
    private Hashtable<String, TrackNode> trackTable;
    private int id;
    public TrackForest(){
        trackTable = new Hashtable<String, TrackNode>();
        id = 0;
    }
    public synchronized String getNewAddress(){
        return Utilities.getMACAddress() + "-" + id++;
    }
    public TrackNode createTrack(String enclosedWave, String suspendedTail, WaveEnvironment env){
        String address = getNewAddress();
        TrackNode tn = new TrackNode(address, enclosedWave, suspendedTail, env);
        trackTable.put(address, tn);
        return tn;
    }
    public TrackNode createTrack(String address, String enclosedWave, String suspendedTail, WaveEnvironment env){
        TrackNode tn = new TrackNode(address, enclosedWave, suspendedTail, env);
        trackTable.put(address, tn);
        return tn;
    }
    public TrackNode createRemoteTrack(String address, String hostAddress, WaveEnvironment waveEnv){
        TrackNode tn = new TrackNode(address, null, null, waveEnv);
        tn.setHost(hostAddress);
        trackTable.put(address, tn);
        return tn;
    }
    public String addTrack(TrackNode tn){
        String address = getNewAddress();
        trackTable.put(address, tn);
        tn.setAddress(address);
        return address;
    }
    public void removeTrack(String address){
        TrackNode track = this.trackTable.remove(address);
        if(track != null)  track.destroy();
    }
    public TrackNode getTrackNode(String address){
        return trackTable.get(address);
    }
    public int numberOfTrackNode(){
        return trackTable.size();
    }
    public void flush(){
        trackTable.clear();
    }
}
